
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  utility;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Hashtable;

import logger.PoolLogger;

import utility.Input;
import utility.MyDate;
import database.RecordSearch;
import dbmanager.*;
//import file.SearchRecord;

/**
 * @author Shweta
 *
 */

public class PeriodDate
{
	
	GetIdName gid;
	private String DaysId;
	private String Pattern="yyyy-MM-dd hh:mm:ss";	
	private Date date;

	PoolLogger pl;
		
		public PeriodDate()
		{
			// logger
			pl = (PoolLogger) PoolLogger.getLogger(this.getClass().getName());
		}
		
	
		public String getNextDate(String period,String currentDate)
		{
			gid=new GetIdName();
			DaysId=gid.getId("Days");	
			
			//if(currentDate.contains(":"))
						Pattern="yyyy-MM-dd";
			
			SimpleDateFormat sdm = new SimpleDateFormat(this.Pattern);
			String TempPeriod=period;
			String strPV=null;
			String resultDate=null;
			try
			{
				date=sdm.parse(currentDate.trim());	
				
				String PeriodId=(TempPeriod);
				
				
				Hashtable<Object, Object>  hasht = new Hashtable<Object, Object>();
				hasht.put("mid",PeriodId);
				hasht.put("pid",DaysId);
				
				//String input_path = Input.MASTERREPORTSPATH + PeriodId + "//" + PeriodId + "_mid" + Input.FILRFORMAT;
				/*SearchRecord sr = new SearchRecord(input_path);
				sr.fileOpen();
				String line[] = sr.getRecordSet(hasht);
				sr.fileClose();*/
				hasht.put("td","null");
				RecordSearch ps = new RecordSearch();
				ps.setConditionMap(hasht);
				String line[] = ps.getArrayFromResultSet();
				for(int i=0;i<line.length;i++)
				{
					String Split[] =  line[i].split("\t");;
					strPV=Split[Column.pv_index-1];
					if(!strPV.equals("0"))
						strPV=gid.getItem(strPV);					
					else
						strPV=Split[Column.vt_index-1];
				}			
				 
				int intDays=Integer.parseInt(strPV);
				resultDate=getNextDate(date, intDays);
				
				//System.out.println(resultDate);
			}
			catch (Exception e) {
				e.printStackTrace();
			}
			return resultDate;
		}
		
	
		 private String getNextDate(Date date,int totalDays)
		 {
			 Date tempDate=date;
			 String str=null;
			 String time=" ";
			try
			{
				SimpleDateFormat sdm = new SimpleDateFormat(this.Pattern);
				String NextDate = sdm.format(tempDate);
				try
				{
				String []split=NextDate.split(" ");
				for(int i=0;i<split.length;i++)
					System.out.println(split[i]);
					time=split[1].trim();
				}
				catch (Exception e) {
					// TODO: handle exception
				}
				MyDate myDate=new MyDate();
				myDate=myDate.getNextDate(NextDate,totalDays);
				str=myDate.getDate();
				str=str+" "+time;
				System.out.println(str);
				//tempDate=sdm.parse(str);
			}
			catch (Exception e) {
				// TODO: handle exception
			}
			return str ;
		 }
		
		 
		public Date getDate()
		{
			return date;
		}
		
	
		public int compareDate(String date1,String date2)
		{
			String str1=date1;
			String str2=date2;
			int i=0;
			Pattern="yyyy-MM-dd hh:mm";
			if(!date1.contains(":")||!date2.contains(":"))
				Pattern="yyyy-MM-dd";
			SimpleDateFormat sdm = new SimpleDateFormat(this.Pattern);
			try
			{
				Date tempDate1=sdm.parse(str1);	
				Date tempDate2=sdm.parse(str2);
				
				if(tempDate1.after(tempDate2))
						i = 1;
				else if(tempDate1.before(tempDate2))
						i = -1;
			}
			catch (Exception e) 
			{
			}
			return i;
		}
		
		public int differncebetweenDate(String date1,String date2)
		{
			String str1=date1;
			String str2=date2;
			int i=0;
			Pattern="yyyy-MM-dd";
			SimpleDateFormat sdm = new SimpleDateFormat(this.Pattern);
			try
			{
				Date tempDate1=sdm.parse(str1);	
				Date tempDate2=sdm.parse(str2);
				if(tempDate1.compareTo(tempDate2)<0)
				{
					Date temp = tempDate1;
					tempDate1 =tempDate2;
					tempDate2 =temp;
				}
				int daysBetween = 0;
				Calendar c = new GregorianCalendar(tempDate1.getYear(), tempDate1.getMonth(), tempDate1.getDate());
				Calendar d = new GregorianCalendar(tempDate2.getYear(), tempDate2.getMonth(), tempDate2.getDate());
				while (c.get(Calendar.YEAR) != d.get(Calendar.YEAR)) {
				    daysBetween += 360;
				    d.add(Calendar.DAY_OF_YEAR, 360);
				}
				daysBetween += c.get(Calendar.DAY_OF_YEAR) - d.get(Calendar.DAY_OF_YEAR);
				System.out.println(daysBetween);
				return daysBetween;
			}
			catch (Exception e) 
			{
				}
			return i;
		}

		public boolean compareMonth(String date1,String date2)
		{
			String str1=date1;
			String str2=date2;
			int i=0;
			Pattern="yyyy-MM-dd hh:mm";
			SimpleDateFormat sdm = new SimpleDateFormat(this.Pattern);
			try
			{
				Date tempDate1=sdm.parse(str1);	
				Date tempDate2=sdm.parse(str2);
				if(tempDate1.getMonth()==tempDate2.getMonth())
					return true;	
			}
			catch (Exception e) {
			}
			return false;
		}
		public int compareDate(String date1,String date2,String Pattern)
		{
			String str1=date1;
			String str2=date2;
			int i=0;
			
			SimpleDateFormat sdm = new SimpleDateFormat(this.Pattern);
			try
			{
				Date tempDate1=sdm.parse(str1);	
				Date tempDate2=sdm.parse(str2);
				
				if(tempDate1.after(tempDate2))
						i = 1;
				else if(tempDate1.before(tempDate2))
						i = -1;
			}
			catch (Exception e) {
				
			}
			return i;
		}
		
		public static void main(String[] args) 
		{
			PeriodDate dateObj=new PeriodDate();
			String period="Monthly";
			//Date currentDate=new Date();
			String currentDate2="2007-10-03 12:00:00";
			String currentDate="2006-1-03 12:05:10";
		//	System.out.println(currentDate);
			
			
			
			System.out.println("Value is "+dateObj.getNextDate(currentDate, currentDate2));
		}

}
